package com.opensky.module.sys.interfaces.facade.v1;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.opensky.common.infrastructure.util.DataBeanUtil;
import com.opensky.common.interfaces.frm.CommonIdFrm;
import com.opensky.module.sys.interfaces.assembler.SysDictAssembler;
import com.opensky.module.sys.interfaces.frm.SysDictQueryFrm;
import com.opensky.module.sys.interfaces.vo.SysDictTreeVO;
import com.opensky.module.sys.interfaces.vo.SysDictVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.assertj.core.util.Lists;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典表(表sys_dict)Facade门面
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2021-11-23 15:31:39
 */
@Api(tags = {"字典表Facade扩展"})
@RestController
@RequestMapping("/extend/sysDict")
public class SysDictFacadeExtend {

    @Resource
    private SysDictAssembler assembler;

    /**
     * 方法说明：查询根目录的一级子树
     *
     * @return {@link List<SysDictTreeVO> }
     */
    @ApiOperationSupport(author = "🇨🇳中国印🇨🇳", order = 10)
    @ApiOperation("1查询根目录的一级子树")
    @PostMapping("/getSubRootList")
    public List<SysDictTreeVO> getSubRootList() {
        List<SysDictTreeVO> retList = Lists.newArrayList();
        List<SysDictVO> subList = assembler.getList(SysDictQueryFrm.builder().parentId(0L).build());
        if (CollectionUtils.isEmpty(subList)) {
            return Lists.newArrayList();
        }
        List<String> subIdList = subList.stream().map(SysDictVO::getId).collect(Collectors.toList());
        subIdList.forEach(id -> {
            SysDictTreeVO vo = assembler.getDictDetail(CommonIdFrm.builder().id(id).build());
            if (vo != null) {
                retList.add(vo);
            }
        });
        return retList;
    }

    /**
     * 方法说明：查询字典一级子树
     *
     * @param frm 请求参数frm对象
     * @return {@link SysDictTreeVO }
     */
    @ApiOperationSupport(author = "🇨🇳中国印🇨🇳", order = 20)
    @ApiOperation("2查询字典一级子树")
    @PostMapping("/getSubDictList")
    public SysDictTreeVO getSubDictList(@RequestBody @Validated CommonIdFrm frm) {
        SysDictVO currentNode = assembler.findById(frm);
        if (currentNode == null) {
            return null;
        }
        SysDictTreeVO sysDictTreeVO = DataBeanUtil.copyOf(currentNode, SysDictTreeVO.class, true);
        List<SysDictVO> children = assembler.getList(SysDictQueryFrm.builder().parentId(Long.parseLong(frm.getId())).build());
        if (CollectionUtils.isEmpty(children)) {
            sysDictTreeVO.setChildren(Lists.newArrayList());
        } else {
            List<SysDictTreeVO> treeList = Lists.newArrayList();
            for (SysDictVO child : children) {
                SysDictTreeVO childVO = DataBeanUtil.copyOf(child, SysDictTreeVO.class, true);
                childVO.setChildren(Lists.newArrayList());
                treeList.add(childVO);
            }
            sysDictTreeVO.setChildren(treeList);
        }
        return sysDictTreeVO;
    }

    /**
     * 方法说明：查询字典所有子树
     *
     * @param frm 请求参数frm对象
     * @return {@link List<SysDictTreeVO> }
     */
    @ApiOperationSupport(author = "🇨🇳中国印🇨🇳", order = 30)
    @ApiOperation("3查询字典所有子树")
    @PostMapping("/getAllSubDictList")
    public List<SysDictTreeVO> getAllSubDictList(@RequestBody @Validated CommonIdFrm frm) {
        List<SysDictTreeVO> retList = Lists.newArrayList();
        SysDictTreeVO vo = assembler.getDictDetail(frm);
        if (vo != null) {
            retList.add(vo);
        }
        return retList;
    }

}

